package daos

import (
	"gorm.io/gorm"
	"sanrenx.com/xyxf/app/models"
	"sanrenx.com/xyxf/pkg/orm"
	"strconv"
)

type SutraDao struct {
	DB *gorm.DB
}

/**
 * @Description: 获取分类下藏经数量
 * @receiver dao
 * @param categoryIds 分类ID数组
 * @param exclude 是否排除
 * @return count 数量
 * @return err 异常信息
 */
func (dao *SutraDao) GetSutraCountByCategory(categoryIds []int, exclude bool) (count int64, err error) {
	tx := dao.DB.Table("sutra").
		Select([]string{
			"id",
		})

	isNotAll := true              //是否分类0：查询全部分类
	cIdsCount := len(categoryIds) //分类数量
	for _, cId := range categoryIds {
		if cId == 0 {
			isNotAll = false
			break
		}
	}
	// 非全部
	if isNotAll && cIdsCount > 0 {
		if exclude { //排除
			tx = tx.Where("category_id NOT IN ?", categoryIds)
		} else {
			if cIdsCount == 1 {
				tx = tx.Where("category_id = ?", categoryIds[0])
			} else {
				tx = tx.Where("category_id IN ?", categoryIds)
			}
		}
	}

	if err = tx.Where("deleted_at is NUll"). //未删除
							Count(&count).
							Error; err != nil && err != gorm.ErrRecordNotFound {
		return 0, err
	}

	return count, nil
}

/**
 * @Description: 获取分类下藏经
 * @receiver dao
 * @param offset 起始
 * @param size 数量
 * @param categoryIds 分类ID数组
 * @param exclude 排除
 * @param orderBy 排序
 * @return ids 文章ID数组
 * @return list 文章column数组
 * @return err 异常信息
 */
func (dao *SutraDao) GetPageSutraColumnByCategory(offset int, size int, categoryIds []int, exclude bool, orderBy string) (ids []string, list []*orm.Column, err error) {
	tx := dao.DB.Table("sutra").
		Select([]string{
			"id",
			"hot",
			"created_at",
		})

	isNotAll := true              //是否分类0：查询全部分类
	cIdsCount := len(categoryIds) //分类数量
	for _, cId := range categoryIds {
		if cId == 0 {
			isNotAll = false
			break
		}
	}
	// 非全部
	if isNotAll && cIdsCount > 0 {
		if exclude { //排除
			tx = tx.Where("category_id NOT IN ?", categoryIds)
		} else {
			if cIdsCount == 1 {
				tx = tx.Where("category_id = ?", categoryIds[0])
			} else {
				tx = tx.Where("category_id IN ?", categoryIds)
			}
		}
	}

	order := orm.OrderByNewDesc
	if orderBy == "hot" {
		order = orm.OrderByHotDesc
	}

	if err = tx.Where("deleted_at is NUll"). //未删除
							Order(order).
							Offset(offset).
							Limit(size).
							Find(&list).
							Error; err != nil && err != gorm.ErrRecordNotFound {
		return nil, nil, err
	}

	//提出IDs
	for _, item := range list {
		ids = append(ids, strconv.FormatUint(item.ID, 10))
	}

	return ids, list, nil
}

/**
 * @Description: 藏经详情
 * @receiver dao
 * @param id 文章ID
 * @return data 文章
 * @return err 异常信息
 */
func (dao *SutraDao) GetSutra(id uint64) (*models.SutraDetails, error) {
	//文章模型
	var data models.SutraDetails
	// 查询数据
	if err := dao.DB.Unscoped().
		Table("sutra").
		Where("id = ?", id).
		First(&data).
		Error; err != nil {
		//无数据
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		}
		return nil, err
	}

	return &data, nil
}

/**
 * @Description: 根据藏经ID获取章回列表
 * @receiver dao
 * @param pageNum 起始
 * @param pageSize 终止
 * @param sutraID 佛经ID
 * @return list 章回列表
 * @return err 异常信息
 */
func (dao *SutraDao) GetChaptersBySutra(pageNum int, pageSize int, sutraID uint64) (ids []string, list []*orm.Column, err error) {
	tx := orm.DB.Table("sutra_chapter").
		Select([]string{
			"id",
			"hot",
			"created_at",
		})

	err = tx.Where("sutra_id", sutraID).
		Where("deleted_at is NULL").
		Order("id ASC").
		Offset(pageNum).
		Limit(pageSize).
		Find(&list).
		Error

	//无记录
	if err != nil && err == gorm.ErrRecordNotFound {
		return nil, nil, nil
	}

	//提出IDs
	for _, item := range list {
		ids = append(ids, strconv.FormatUint(item.ID, 10))
	}

	return ids, list, err
}

/**
 * @Description: 获取章回详情
 * @receiver dao
 * @param id
 * @return *chapter.ChapterDetails
 * @return error
 */
func (dao *SutraDao) GetChapter(id uint64) (*models.ChapterDetails, error) {
	var data models.ChapterDetails
	// 查询数据
	err := orm.DB.Unscoped().
		Table("sutra_chapter").
		Where("id = ?", id).
		First(&data).
		Error

	//无记录
	if err != nil && err == gorm.ErrRecordNotFound {
		return nil, nil
	}

	return &data, err
}
